package api

import (
	"cr.org/constant"
	"cr.org/model"
	"cr.org/modules/service"
	"github.com/gin-gonic/gin"
	log "github.com/sirupsen/logrus"
)

type otherApi struct {
}

func (a otherApi) GetAiModelDetail(c *gin.Context) {
	arg := struct {
		Id int64 `json:"id,string"`
	}{}
	var (
		err error
	)
	if err = c.ShouldBindJSON(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	m, err := service.OtherService.GetAiModelDetail(arg.Id)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok(c, m)
}

func (a otherApi) SaveAiModels(c *gin.Context) {
	var (
		m   model.AiModelsT
		err error
	)
	if err = c.ShouldBindJSON(&m); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	err = service.OtherService.SaveAiModels(m)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (a otherApi) UpdateThumbsUpAddOne(c *gin.Context) {
	arg := struct {
		Id int64 `json:"id,string"`
	}{}
	var (
		err error
	)
	if err = c.ShouldBindJSON(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	err = service.OtherService.UpdateThumbsUpAddOne(GetCurrentUser(c).UserID, arg.Id)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (a otherApi) CheckThumbsUpStatus(c *gin.Context) {
	arg := struct {
		Id int64 `json:"id,string"`
	}{}
	var (
		err error
		b   bool
	)
	if err = c.ShouldBindJSON(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	b, err = service.OtherService.CheckThumbsUpStatus(GetCurrentUser(c).UserID, arg.Id)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok(c, b)
}

func (a otherApi) SaveAiDraw(c *gin.Context) {
	var (
		m      model.AiDrawT
		selfId int64
		err    error
	)
	if err = c.ShouldBindJSON(&m); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	selfId = GetCurrentUser(c).UserID
	err = service.OtherService.SaveAiDraw(m, selfId)
	if err != nil {
		BadWithError(c, err)
		return
	}
	Ok204(c)
}

func (a otherApi) GetAiDrawTop5(c *gin.Context) {
	var (
		arg    model.SearchAiDraw
		selfId int64
	)
	if err := c.ShouldBindQuery(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	selfId = GetCurrentUser(c).UserID
	li, err := service.OtherService.GetAiDrawTop5(arg, selfId)
	if err != nil {
		log.Error(err)
		BadWithError(c, err)
		return
	}
	Ok(c, li)
}

func (a otherApi) AiDrawPage(c *gin.Context) {
	var (
		arg    model.SearchAiDraw
		selfId int64
	)
	if err := c.ShouldBindQuery(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	selfId = GetCurrentUser(c).UserID
	li, total, err := service.OtherService.AiDrawPage(arg, selfId)
	if err != nil {
		log.Error(err)
		BadWithError(c, err)
		return
	}
	Ok(c, gin.H{"li": li, "total": total})

}

func (a otherApi) AiModelsPage(c *gin.Context) {
	var arg model.SearchAiModels
	if err := c.ShouldBindQuery(&arg); err != nil {
		log.Error(err)
		BadWithCode(c, constant.ParamsError)
		return
	}
	li, total, err := service.OtherService.AiModelsPage(arg)
	if err != nil {
		log.Error(err)
		BadWithError(c, err)
		return
	}
	Ok(c, gin.H{"li": li, "total": total})
}
